function Pager(selector){
    this.currentPage = getCurrentPage();
    this.maxPage = getMaxPage();
    this.firstPageNo = 1;;
    
    this.firstPage = function(){
        this.goPage(this.firstPageNo);
    };
    
    this.priorPage = function(){
        var page = this.currentPage - 1;
        if(page < 1){page = 1;}
        this.goPage(page);
    };
    
    this.nextPage = function(){
        var page = this.currentPage + 1;
        if(page > this.maxPage){page = this.maxPage;}
        this.goPage(page);
    };
    
    this.lastPage = function(){
        this.goPage(this.maxPage);
    };
    
    this.setPage = function(obj){
        var page = parseInt(obj.prev().val());
        if(page < 1){
            page = 1;
        }
        this.goPage(page);
    }
    
    this.goPage = function (page){
        var url = window.location.href;
        var u = getPageUrl(url, page);
        goUrl(u);
    };
    
    function getCurrentPage(){
        return parseInt($(selector).attr('currentPage'));
    };
    
    function getMaxPage(){
        return parseInt($(selector).attr('maxPage'));
    }
    
    function goUrl(url){
        window.location.href = url;
    }
    
    function getPageUrl(url, page){
        if(isNaN(page)){
            page = 1
        }
        var urls = url.split('&');
        var str = '';
        var len = urls.length;
        var ss = false;
        for(var i = 0; i < len; i++){
            var s = urls[i].split('=');
            if(s[0] == 'page'){
                ss = true;
                urls[i] = s[0] + '=' + page;
            }
            str += '&' + urls[i];
        }
        if(!ss){
            str += '&page='+page;
        }
        return str.substr(1);
    }
}

$(document).ready(function(){
    $('.firstPage').click(function(){
        var pager = new Pager('.pager');
        pager.firstPage();
    });
    $('.priorPage').click(function(){
        var pager = new Pager('.pager');
        pager.priorPage();
    });
    $('.nextPage').click(function(){
        var pager = new Pager('.pager');
        pager.nextPage();
    });
    $('.lastPage').click(function(){
        var pager = new Pager('.pager');
        pager.lastPage();
    });
    $('.setPage').click(function(){
        var pager = new Pager('.pager');
        pager.setPage($(this));
    })
});